package com.mall.permission.service.impl;

import com.mall.permission.model.Permission;
import com.mall.permission.mapper.PermissionMapper;
import com.mall.permission.service.IPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * URL路径信息 服务实现类
 * </p>
 *
 * @author Darren
 * @since 2022-10-20
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean addPermission(Permission permission){
        return super.save(permission);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteById(String id){
        return super.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updatePermission(Permission permission){
        return super.updateById(permission);
    }

    @Override
    public Permission getById(String id){
        return super.getById(id);
    }

    @Override
    public List<Permission> selectList(Permission permission){
        return super.list(getQueryWrapper(permission));
    }

    @Override
    public Page<Permission> selectPage(Page<Permission> page, Permission permission){
        return super.page(page, getQueryWrapper(permission));
    }

    @Override
    public List<Permission> findByMatch(int match) {
        return this.list(new LambdaQueryWrapper<Permission>().eq(Permission::getUrlMatch, match));
    }

    public LambdaQueryWrapper<Permission> getQueryWrapper(Permission permission){
        return Wrappers.<Permission>lambdaQuery()
            .eq(ObjectUtils.isNotEmpty(permission.getId()), Permission::getId, permission.getId())
            .eq(ObjectUtils.isNotEmpty(permission.getSourceName()), Permission::getSourceName, permission.getSourceName())
            .eq(ObjectUtils.isNotEmpty(permission.getUrl()), Permission::getUrl, permission.getUrl())
            .eq(ObjectUtils.isNotEmpty(permission.getUrlMatch()), Permission::getUrlMatch, permission.getUrlMatch())
            .eq(ObjectUtils.isNotEmpty(permission.getServiceName()), Permission::getServiceName, permission.getServiceName())
            .eq(ObjectUtils.isNotEmpty(permission.getMethod()), Permission::getMethod, permission.getMethod())
        ;
    }
}
